//
//  YHPMainTabBar.swift
//  SinaSwift
//
//  Created by Yan on 16/3/6.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit

class YHPMainTabBar: UITabBar {

    // xcod默认,调用此方法 开发需要使用纯代码开发
    override init(frame: CGRect) {
        super.init(frame: frame)

    }
    
    // 由于storyboard是xml格式 和纯代码编码不一样因此storyboard调用此方法
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        // 循环添加tabbarbutton 空出位子留给plusButton
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        var index : CGFloat = 0
        for subview in subviews {
            if subview .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRectOffset(rect, w * index - w, 0)
            }
            // index == 2 时 index += 2 否则index += 1
            index += (index == 2 ? 2 : 1)
        }
        
        addSubview(plusButton)
        plusButton.frame = CGRectOffset(rect, w * 2, 0)
        
    }
    
//    lazy var plusButton: UIButton = buttonWithImage("tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    lazy var plusButton: UIButton = UIFactory.factoryButtonWithImage("tabbar_compose_icon_add", backImageName: "tabbar_compose_button")

   
}
